package com.csun.cmny.provider.utils;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class JuheWeatherUtil {

    /**
     * 聚合数据获取天气数据，传入参数为城市名称
     * @param: []
     * @return: net.sf.json.JSONObject
     */
    public static String getWeather(){

        /**
         * @date    2020-05-13
         * @author  chisj
         * @desc    聚合数据次数不够使用，替换阿里云
         */
//        //聚合地址 cityname为城市名 需要utf8 urlencode  key为接口密钥
//        String key = "1f5290e9c3271263f40cdb4d83af68c6";
//        String url = "http://v.juhe.cn/weather/index?cityname=%E6%AD%A6%E6%B1%89&dtype=&format=&key=" + key;
//        String json = loadJSON(url);
//        JSONObject obj = JSONObject.fromObject(json);
//        return obj ;

//        String host = "https://jisutqybmf.market.alicloudapi.com";
        String host = "https://jisuqgtq.market.alicloudapi.com";
        String path = "/weather/query";
        String method = "GET";
        String appcode = "4d75352d93c045558b4b4d945ee75955";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("city", "武汉");
//        querys.put("citycode", "citycode");
//        querys.put("cityid", "cityid");
//        querys.put("ip", "ip");
//        querys.put("location", "location");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
//            System.out.println(response.toString());
            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity()));
            String json = EntityUtils.toString(response.getEntity());
//            JSONObject obj = JSONObject.fromObject(json);
            return json ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取数据
    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
        }
        return json.toString();
    }

    //

    @Test
    public void main() {

        System.out.println("weather = " + getWeather().toString());

    }

    @Test
    public void test() {
//        String host = "https://jisutqybmf.market.alicloudapi.com";
        String host = "https://jisuqgtq.market.alicloudapi.com";
        String path = "/weather/query";
        String method = "GET";
        String appcode = "4d75352d93c045558b4b4d945ee75955";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("city", "武汉");
//        querys.put("citycode", "citycode");
//        querys.put("cityid", "cityid");
//        querys.put("ip", "ip");
//        querys.put("location", "location");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
